﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

namespace RichLabelSpace
{
    public class RichLabel : RichLabelComponent
	{
        // 文本属性，如果是空表示创建的Label是默认值
        private string m_font = null;
        private int m_fontsize = 18;
        private FontStyle m_fontstyle = FontStyle.Normal;
        private string m_text = null;
        private UILabel m_label = null;
        private string color_str = null;
        private Transform transform;
        private RichLabelCtrl richLabelCtrl;
        private FontStyle fontStyle;
        private RichLabelEvent richLabelEvent;

        public string Text{ get { return m_text; } }
        protected override void SetProperty(string[] property)
        {
            if(property[0].Equals("font"))
            {
                this.m_font = property[1];
            }
            else if(property[0].Equals("fontsize"))
            {
                this.m_fontsize = int.Parse(property[1]);
            }
            else if(property[0].Equals("fontstyle"))
            {
                this.m_fontstyle = (FontStyle)Enum.Parse(typeof(FontStyle), property[1]);
            }
            else if(property[0].Equals("text"))
            {
                this.m_text = RichTxtTool.ReplaceSpecialSymbolBack(property[1]);
            }
            else if (property[0].Equals("color"))
            {
                this.color_str = property[1];
            }
        }
        public RichLabel[] SubComponent(string[] lines)
        {
            // 只有两行以上才能切割
            if (lines != null && lines.Length > 1)
            {
                var list = new List<RichLabel>();

                for (int i = 0; i < lines.Length; ++i)
                {
                    if (i == 0)
                    {
                        // 重置UI 和 事件
                        this.m_text = lines[i];
                        Excute();
                        this.m_rich_event.Init();
                        ExcuteSelf();
                    }
                    else
                    {
                        var temp_rich_comp = new RichLabel(this.m_ctrl.GameObj.transform, this.m_ctrl, this.m_font, this.m_fontsize, this.m_fontstyle, this.color_str, lines[i], this.m_rich_event);
                        if (temp_rich_comp != null) list.Add(temp_rich_comp);
                    }
                }

                return list.ToArray();
            }

            return null;
        }

        public RichLabel(Transform parent, RichLabelCtrl ctrl)
            : base(parent,ctrl) 
        {
            var go = new GameObject();
            go.name = "RichLabel_" + go.GetInstanceID();
            RichLabelCtrl.ResetGameObj(go, parent);
            this.m_label = go.AddComponent<UILabel>();
            m_ui_widget = this.m_label;
        }

       public RichLabel(Transform parent, RichLabelCtrl ctrl,string font,int font_size,FontStyle fontstyle, string color_str,string text,RichLabelEvent rich_event)
            : base(parent,ctrl) 
        {
            var go = new GameObject();
            go.name = "RichLabel" + go.GetInstanceID();
            go.transform.SetParent(parent);
            this.m_label = go.AddComponent<UILabel>();
            this.m_ui_widget = this.m_label;
            this.m_font = font;
            this.m_fontsize = font_size;
            this.m_fontstyle = fontstyle;
            this.color_str = color_str;
            this.m_text = text;
            this.m_component_type = RichComponentType.Label;
            Excute();
            this.m_rich_event = new RichLabelEvent(rich_event, this);
            this.m_rich_event.Init();
            ExcuteSelf();
        }

        public override void Excute()
        {
            this.m_label.bitmapFont = Resources.Load<UIFont>(this.m_font);
            this.m_label.color = new Color32(255, 255, 255, 255);
            this.m_label.fontSize = this.m_fontsize;
            this.m_label.fontStyle = this.m_fontstyle;
            this.m_label.alignment = NGUIText.Alignment.Left;
            this.m_label.height = this.m_fontsize;
            this.m_label.pivot = UIWidget.Pivot.Left;
            this.m_label.overflowMethod = UILabel.Overflow.ResizeFreely;
            this.m_label.maxLineCount = 1;
            this.m_label.text = string.IsNullOrEmpty(this.color_str) ? this.m_text : "[" + this.color_str + "]" + this.m_text + "[-]";
            RichLabelCtrl.ResetGameObj(this.m_ui_widget.gameObject, this.m_parent);
            //this.m_label.MakePixelPerfect();
        }

        // 被点击后变颜色
        public override void OnClick()
        {
            this.m_label.color = new Color32(100 , 5 , 120 ,255);
        }
    }
}

